package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/**
 * 项目规划及管理
 * 上海大数据学院院长 ：孙丰朝
 * 技术指导及需求分析：郭洵
 * 编程：楚志高
 *
 * @author bawei  bigdata sh
 * @since @Date: 2022/6/27 8:47
 */
@Configuration
public class WebSecurity extends WebSecurityConfigurerAdapter {

    @Resource
    UserDetailsService userServicejdbc ;

    //通过动态服务区进行检测

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(this.userServicejdbc).passwordEncoder(password());
//    }
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests().antMatchers("/user" ,"/proceslogin",
//                "/user/nologin","/user/loginFiailed")
//                .permitAll()  // 批准
//                .and().formLogin().loginPage("/user/nologin")
//                .loginProcessingUrl("/proceslogin") //获取用户名与密码
//                .failureForwardUrl("/user/loginFiailed");
//             //进行跨域 保存到内存中
//        http.headers().cacheControl(); //保存在内存中
//        http.cors() ; //跨域
//        http.csrf().disable() ;
//
//
//
//
//    }

    /*
    //采用内存去保存用户名与密码
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //获取加密工具类
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder() ;
        //对密码进行加密
        String encode = bCryptPasswordEncoder.encode("123");
        // 吧真实密码与加密后的密码进行匹配
        boolean matches = bCryptPasswordEncoder.matches("123", encode);

        //给定权限
        auth.inMemoryAuthentication().withUser("user").password(encode).roles("admin") ;

    }
    */

    //
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

        String encode = bCryptPasswordEncoder.encode("123");
        boolean matches = bCryptPasswordEncoder.matches("123", encode);
        auth.inMemoryAuthentication().withUser("user").password(encode).roles("admin");


    }
    @Bean
    PasswordEncoder password(){
        return  new BCryptPasswordEncoder() ;
    }
}
